import api from '../../utils/request/api'
import {
	login
} from '../../utils/request/apiurl'
const app = getApp();

Page({
	data: {
		isAuthorize: true,
		uid: '',
		link: '',
	},
	onLoad: function() {
		const hasAuthorize = wx.getStorageSync('hasAuthorize');
		if (hasAuthorize) {
			this.setData({
				isAuthorize: false
			})
			wx.switchTab({
				url: "../home/home"
			})
		} else {
			this.setData({
				isAuthorize: true
			})
		}
	},
	loginList(userInfo) {
		wx.login({
			success: function(res) {
				console.log(res, 'resresres')
				if (res.code) {
					//发起网络请求
					var options = {
						code: res.code,
						// userInfo:userInfo,
						nickName: userInfo.nickName,
						gender: userInfo.gender,
						avatarUrl: userInfo.avatarUrl,
						country: userInfo.country,
						province: userInfo.province,
						language: userInfo.language,
						city: userInfo.city,
					};
					api.get(login, options).then(res => {
						// debugger
						console.log(res)
						var userInfo={}
						userInfo['uid']=res.id;
						userInfo['link']=res.isLink;
						userInfo['rank']=res.rank;
						userInfo['openid'] = res.openid;
						userInfo['isPayPassword']=res.isPayPassword;
						userInfo['isColonel']=res.isColonel //0不是团长，1是团长
						userInfo['role'] = res.role;//0不是快递员，1是快递员
						userInfo['phone'] = res.phone;
						wx.setStorageSync('userInfo', userInfo);
					}).catch(err => {
						wx.showToast({
							title: err.message,
							icon: 'none'
						})
					})
				} else {
					wx.showToast({
						title: "登录失败",
						icon: "none",
						duration: 1000
					});
				}
			}
		})
	},
	getUserInfo: function(res) {
		console.log(res)
		if (res.detail.userInfo) {
			wx.setStorageSync('hasAuthorize', true);
			this.loginList(res.detail.userInfo);		
			wx.setStorageSync('Info', res.detail);
			wx.switchTab({
				url: "../home/home"
			})
		} else {
			wx.showToast({
				title: '请先授权',
				icon: 'none',
				duration: 3000
			})
		}
	},




})


// const app = getApp();
// 
// Page({
//   data: {
//     isAuthorize: true
//   },
//   onLoad: function() {
//     const hasAuthorize = wx.getStorageSync('hasAuthorize');
//     if (hasAuthorize) {
//       this.setData({
//         isAuthorize: false
//       })
//       wx.switchTab({
//         url: "../home/home"
//       })
//     } else {
//       this.setData({
//         isAuthorize: true
//       })
//     }
//   },
//   getUserInfo: function (res) {
//     if (res.detail.userInfo) {
//       wx.setStorageSync('hasAuthorize', true);
//       wx.setStorageSync('userInfo', res.detail);
//       wx.setStorageSync('uid', '2c9b8091661b7b0701661b8cee070000'); //测试uid
//       wx.switchTab({
//         url: "../home/home"
//       })
//     } else {
//       wx.showToast({
//         title: '请先授权',
//         icon: 'none',
//         duration: 3000
//       })
//     }
//   }
// })
